//
//  RBHomeSecKillView.m
//  zhongzhenglu
//
//  Created by XL on 2022/5/26.
//

#import "RBHomeSecKillView.h"
#import "UIView+RBAdd.h"
#import "RBDefine.h"
#import <YYCategories.h>
#import "RBHomeSecKillItemView.h"
#import "RBBannerView.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import "UIImageView+RBAdd.h"
#import <UIView+YYAdd.h>
#import "RBSecondKillDetailVC.h"

@interface RBHomeSecKillView()<RBBannerViewDelegate>

@property(nonatomic, strong) RBBannerView *bannerView;

@property(nonatomic, strong)IBOutlet UIView *headerView;
@property(nonatomic, strong)IBOutlet UILabel *hourL;
@property(nonatomic, strong)IBOutlet UILabel *minuteL;
@property(nonatomic, strong)IBOutlet UILabel *secondL;
@property(nonatomic, strong)IBOutlet UILabel *timeL;
@property(nonatomic, strong)IBOutlet UIView *contentView;

@property(nonatomic, strong)UIView *indicatorView;
@property(nonatomic, assign)NSInteger totalPages;
@property(nonatomic, assign)NSInteger currentPage;

@property(nonatomic, retain) NSArray *data;

@end

@implementation RBHomeSecKillView

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self zoomSubViewsWithSelf];
    self.headerView.backgroundColor = [UIColor colorWithHexString:@"#518CFF"];
    self.headerView.size = CGSizeMake(kRBMainScreenWidth-Zoom(30), Zoom(43));
    [self.headerView setRounderWithRectCorner:UIRectCornerTopLeft | UIRectCornerTopRight radius:Zoom(8)];
    
    self.bannerView.hidden = NO;
    
//    [self fillData];
}

- (void)receiveDataWith:(NSArray *)data {
    NSMutableArray *tempArrayM = [NSMutableArray arrayWithArray:data];
    NSMutableArray *arrayM = NSMutableArray.array;
    while (tempArrayM.count) {
        NSMutableArray *section = NSMutableArray.array;
        [arrayM addObject:section];
        for (NSInteger i = 0; i < 3; i++) {
            if (tempArrayM.count) {
                [section addObject:tempArrayM.firstObject];
                [tempArrayM removeFirstObject];
            }
        }
    }
    self.data = arrayM;
    [self.bannerView reloadData];
    
    self.totalPages = self.data.count;
    self.currentPage = 0;
}

+ (CGFloat)suggestViewHeight
{
    return Zoom(255);
}

#pragma mark - RBBannerViewDelegate

- (NSInteger)numberOfItemsInBannerView:(RBBannerView *)bannerView
{
    return self.data.count;
}

- (void)bannerView:(RBBannerView *)bannerView willSetupView:(UIView *)view withIndex:(NSInteger)index
{
    [view removeAllSubviews];
    CGFloat itemW = (kRBMainScreenWidth-Zoom(30))/3;
    NSArray *data = self.data[index];
    for (NSInteger i = 0; i < data.count; i++) {
        RBHomeSeckillModel *model = data[i];
        RBHomeSecKillItemView *itemView = [RBHomeSecKillItemView view];
        itemView.model = model;
        itemView.rbExData = model;
        [itemView rbAddTapGesWithTarget:self action:@selector(clickGoDetail:)];
        [view addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(itemW);
            make.left.mas_equalTo(itemW*i);
        }];
    }
}

- (void)bannerView:(RBBannerView *)bannerView scrollToIndex:(CGFloat)index
{
    self.currentPage = (NSInteger)roundf(index);
}

- (void)bannerView:(RBBannerView *)bannerView didSelectView:(UIView *)view withIndex:(NSInteger)index
{
    
}

#pragma mark - Event
//更多
- (IBAction)topBtnTapped
{
    if (self.moreBlock)
    {
        self.moreBlock();
    }
}
//进入详情
- (void)clickGoDetail:(UIGestureRecognizer *)ges {
    RBHomeSeckillModel *model = ges.view.rbExData;
    
    RBSecondKillDetailVC *vc = RBSecondKillDetailVC.new;
    vc.goods_id = model.id;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter

- (void)setTotalPages:(NSInteger)totalPages
{
    _totalPages = totalPages;
    [self.indicatorView removeAllSubviews];
    UIView *leftView = nil;
    for (NSInteger i = 0; i < totalPages; i++)
    {
        UIView *itemView = UIView.new;
        itemView.tag = i;
        [itemView rbSetCornerRadius:Zoom(2.5)];
        [self.indicatorView addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(0);
            if (leftView)
            {
                make.left.mas_equalTo(leftView.mas_right).offset(Zoom(5));
            }
            else
            {
                make.left.mas_equalTo(0);
            }
            if (i == totalPages - 1)
            {
                make.right.mas_equalTo(0);
            }
        }];
        leftView = itemView;
    }
    self.currentPage = 0;
}

- (void)setCurrentPage:(NSInteger)currentPage
{
    _currentPage = currentPage;
    for (UIView *itemView in self.indicatorView.subviews)
    {
        [itemView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(itemView.tag == currentPage? Zoom(15): Zoom(5));
        }];
        itemView.backgroundColor = itemView.tag == currentPage? [UIColor colorWithHexString:@"#518CFE"]: [UIColor colorWithHexString:@"#C1C1C1"];
    }
}


#pragma mark - Getter

- (RBBannerView *)bannerView
{
    if (!_bannerView)
    {
        self.bannerView = ({
            RBBannerView *view = RBBannerView.new;
            view.duration = 1000;
            view.itemViewClass = UIView.class;
            view.deleagte = self;
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _bannerView;
}

- (UIView *)indicatorView
{
    if (!_indicatorView)
    {
        self.indicatorView = ({
            UIView *view = UIView.new;
            view.userInteractionEnabled = NO;
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(Zoom(-13));
                make.height.mas_equalTo(Zoom(5));
                make.centerX.mas_equalTo(0);
            }];
            view;
        });
    }
    return _indicatorView;
}

@end
